﻿using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace WP7Contrib.View.Controls.Behaviors
{
    /// <summary>
    /// This behavior causes the associated element to animate into position
    /// as the page is loaded.
    /// </summary>
    public class FlyInBehaviour : Behavior<FrameworkElement>
    {
        private static double _flyInSpeed = 0.3;

        protected override void OnAttached()
        {
            TranslateTransform trans = new TranslateTransform();
            trans.X = 300;
            trans.Y = -50;
            AssociatedObject.RenderTransform = trans;

            var sb = new Storyboard();

            // animate the X position
            var db = TiltBehaviour.CreateAnimation(300, 0, _flyInSpeed, TranslateTransform.XProperty, trans);
            sb.Children.Add(db);

            // animate the Y position
            db = TiltBehaviour.CreateAnimation(-100, 0, _flyInSpeed, TranslateTransform.YProperty, trans);
            sb.Children.Add(db);

            AssociatedObject.Loaded += (s, e) => AssociatedObject.Dispatcher.BeginInvoke(() => sb.Begin());
        }
    }
}
